package com.example.demo

import com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartHttpServletRequest


@RestController
class UploadControlor {


    @GetMapping("/upload")
    fun upload(): String {
        return "upload"
    }



    //单张图片+文字上传
    @PostMapping("/upload")
    fun upload(part: MultipartHttpServletRequest): String {
        println(2)
        val file = part.getFile("file")!!.name
        print(file)
        val parameter = part.getParameter("name")

        return "我的上传成功！"
    }


    /**
     * json + 其它参数上传
     */
    @PostMapping("/uploadJson")
    fun uploadJson(part: MultipartHttpServletRequest): String {
        println(2)
        return "我的上传成功！"
    }


    /**
     * 测试多文件上传
     */
    @PostMapping("/uploadMoreFile")
    fun uploadMoreFile(part: MultipartHttpServletRequest): String {
        println(2)

        val files = part.getFiles("image")
        files.map {
            println(it.originalFilename)
        }

        val parameterNames = part.parameterNames.toList()

        parameterNames.map {
            val str = part.getParameter(it)
            println(str)
        }
        return "我的上传成功！"
    }
}